#include <iostream>

using namespace std;
// Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
// Do not allocate extra space for another array, you must do this in place with constant memory.
// For example, Given input array A = [1,1,2],
// Your function should return length = 2, and A is now [1,2].

// class Soultion{
// public:
//   int removeDuplicates(int a[],int n){
//     if(n==0) return 0;
//     int index=0,i;
//     for(i = 1;i < n;i++){
//       if(a[index]!=a[i]){
//         a[++index] = a[i];
//       }
//     }
//     return index+1;
//   }
//
// };

int removeDuplicates(int a[],int n){
  if(n==0) return 0;
  int index=0,i;
  for(i = 1;i < n;i++){
    if(a[index]!=a[i]){
      a[++index] = a[i];
    }
  }
  return index+1;
}
int removeDuplicates1(int a[],int n){
  return distance(a,unique(a,a+n));
}

int removeDuplicates2(int a[],int n){

}
// Follow up for ”Remove Duplicates”: What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3],
// Your function should return length = 5, and A is now [1,1,2,2,3]
int removeDuplicates_t(int A[], int n) {
  if (n <= 2) return n;
  int index = 2;
  for (int i = 2; i < n; i++){
      if (A[i] != A[index - 2])
          A[index++] = A[i];
  }
  return index;
}


int main(int argc, char const *argv[]) {
  int a[6]={1,2,3,4,4,5};
  int len;
  len = removeDuplicates1(a,6);
  for(int i=0;i <5; i++){
    cout<<a[i]<<endl;
  }

  cout << len;
  return 0;
}
